package v1

import (
	"github.com/axgle/mahonia"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/util/gutil"
	"regexp"
	"strings"
)

type MyInt interface {
	any
}

type ResultResponse struct {
	Status  int    `json:"Status"`
	Message string `json:"Message"`
	Data    any    `json:"Data"`
	Data2   any    `json:"Data2"`
	Data3   any    `json:"Data3"`
}

type ResultResponse2 struct {
	Status     int    `json:"Status"`
	Message    string `json:"Message"`
	Data       any    `json:"Data"`
	Data2      any    `json:"Data2"`
	Data3      any    `json:"Data3"`
	Total      int    `json:"Total"`
	Count      int    `json:"Count"`
	Rundate    any    `json:"Rundate"`
	ORGNAME    any    `json:"ORGNAME"`
	WEIJIANCE  int
	WEIWANJIAN int
	YIJIANCE   int
	GUZHANG    int
	NOT        int
	NOTCHULI   int
}

func arrMake(str string) string {
	save := []string{}
	//check := 0
	//save := make([]map[string]string, 0)
	for i, v := range str {
		if i%2 != 0 {
			save = append(save, string(str[i-1])+string(v))
		}
	}
	strArrayNew := strings.Join(save, ",")
	strArrayNew = ortherZhuanyi(strArrayNew)
	//gutil.Dump(check)
	return strArrayNew
}

func ortherZhuanyi(str string) string {

	check := containsString(str, "5a,01")
	if check {
		str = strings.Replace(str, "5a,01", "&#1", -1)
	}
	check = containsString(str, "5a,02")
	if check {
		str = strings.Replace(str, "5a,02", "&#2", -1)
	}
	check = containsString(str, "5e,01")
	if check {
		str = strings.Replace(str, "5e,01", "&#3", -1)
	}
	check = containsString(str, "5e,02")
	if check {
		str = strings.Replace(str, "5e,02", "&#4", -1)
	}
	str = zhuanyi2(str)
	str = strings.Replace(str, ",", "", -1)
	return str
}

func zhuanyi2(str string) string {
	str = strings.Replace(str, "&#1", "5b", -1)
	str = strings.Replace(str, "&#2", "5a", -1)
	str = strings.Replace(str, "&#3", "5d", -1)
	str = strings.Replace(str, "&#4", "5e", -1)
	return str
}

func ConvertToString(src string, srcCode string, tagCode string) string {
	srcCoder := mahonia.NewDecoder(srcCode)
	srcResult := srcCoder.ConvertString(src)
	tagCoder := mahonia.NewDecoder(tagCode)
	_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
	result := string(cdata)
	return result
}

func containsString(s, substr string) bool {
	return strings.Contains(s, substr)
}

func zhuanyi(str string) string {
	check := containsString(str, "5a01")
	if check {
		str = strings.Replace(str, "5a01", "&#1", 1)
	}
	check = containsString(str, "5a02")
	if check {
		str = strings.Replace(str, "5a02", "&#2", 1)
	}
	check = containsString(str, "5e01")
	if check {
		str = strings.Replace(str, "5e01", "&#3", 1)
	}
	check = containsString(str, "5e02")
	if check {
		str = strings.Replace(str, "5e02", "&#4", 1)
	}
	str = zhuanyi2(str)
	return str
}

func TestEncoder(str string) string {
	// 编码UTF-8成GBK
	//enc := mahonia.NewEncoder("gbk")
	//str_gbk := enc.ConvertString(str)
	//return str_gbk
	//fmt.Println(str_gbk)
	// 解码GBK为UTF-8
	decoder := mahonia.NewDecoder("gbk")
	//str_utf8 := decoder.ConvertString(str_gbk)
	str_utf8 := decoder.ConvertString(str)
	return str_utf8
}

func rmu0000(s string) string {
	str := make([]rune, 0, len(s))
	for _, v := range []rune(s) {
		if v == 0 {
			continue
		}
		str = append(str, v)
	}
	return string(str)
}

func takeIt() {
	res := 123
	//res, _ := g.DB().Model("fa_daoru").All()
	gutil.Dump(res)
}

func respose[T MyInt](r *ghttp.Request, code int, message string, data T) {
	var response ResultResponse
	response = ResultResponse{Status: code, Message: message, Data: data}
	r.Response.WriteJsonP(response)
}

func respose2[T MyInt](r *ghttp.Request, code int, message string, data T, data2 T, data3 T) {
	var response ResultResponse
	response = ResultResponse{Status: code, Message: message, Data: data, Data2: data2, Data3: data3}
	r.Response.WriteJsonP(response)
}

func respose3[T MyInt](r *ghttp.Request, code int, message string, data T, total int, count int, Rundate any, ORGNAME any, WEIJIANCE int, WEIWANJIAN int, YIJIANCE int, GUZHANG int, not int, notchuli int) {
	var response ResultResponse2
	response = ResultResponse2{Status: code, Message: message, Data: data, Total: total, Count: count, Rundate: Rundate, ORGNAME: ORGNAME, WEIJIANCE: WEIJIANCE, WEIWANJIAN: WEIWANJIAN, YIJIANCE: YIJIANCE, GUZHANG: GUZHANG, NOT: not, NOTCHULI: notchuli}
	r.Response.WriteJsonP(response)
}

func isNumeric(str string) bool {
	reg := regexp.MustCompile("^[0-9]+$")
	return reg.MatchString(str)
}
